package com.zhf.shardingDemo.spiextention;

import org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicLong;

public class MyKeyGenerator implements ShardingKeyGenerator {

    private AtomicLong atom = new AtomicLong(0);

    @Override
    public Comparable<?> generateKey() {
        LocalDateTime ldt = LocalDateTime.now();
        String timestamp = DateTimeFormatter.ofPattern("HHmmssSSS").format(ldt);
        return Long.parseLong(timestamp + atom.incrementAndGet());
    }

    @Override
    public String getType() {
        return "MYKEY";
    }

    @Override
    public Properties getProperties() {
        return null;
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
